package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.ruoyi.framework.tenant.TenantProperties;
import com.ruoyi.framework.tenant.db.TenantDatabaseInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * Mybatis支持*匹配扫描包
 * 
 * @author ruoyi
 */
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan("${mybatis-plus.mapperPackage}")
public class MyBatisConfig
{
    @Autowired
    private Environment env;

//    @Bean
//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource,MybatisPlusInterceptor interceptor) throws Exception {
//        // 搜索指定包别名
//        String typeAliasesPackage = env.getProperty("mybatis-plus.type-aliases-package");
//        // 配置mapper的扫描，找到所有的mapper.xml映射文件
//        String mapperLocations = env.getProperty("mybatis-plus.mapper-locations");
//        // 加载全局的配置文件
//        String configLocation = env.getProperty("mybatis-plus.config-location");
//
//        // final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
//
//        // 乐观锁（按需配置）
////        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        // 分页插件
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//        // 如果不在这里设置插件，在使用mybatis-plus分页功能时，sql不会拼接分页参数，总是查询所有数据
//        sessionFactory.setPlugins(interceptor);
//        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
//        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
//        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
//        return sessionFactory.getObject();
//    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(TenantProperties tenantProperties) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (tenantProperties.getEnable()) {
            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(tenantProperties)));
        }
        return interceptor;
    }
}